/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fasea.search;

/**
 * Unterucht die Eingabe des Nutzers und optimiert diese fuer die Suche in Lucene.
 * @author lars-wolfram
 */
class InputExaminer {
    
    private static String LUCENE_WILDCARDS_SINGLE_CHARACTER = "?";
    private static String LUCENE_WILDCARDS_MULTI_CHARACTER = "*";
    
    public static String getOptimizedSearchString(String userInput){
        if(userInput == null){
            return "";
        }else{
            userInput = replaceWildcardsAtBeginning(userInput);
            userInput = replaceWildcardsAfterWithspace(userInput);
            return userInput;
        }
    }
    
    public static String getOptimizedSearchStringForFullSearch(String userInput){
        return getOptimizedSearchString(userInput) + LUCENE_WILDCARDS_MULTI_CHARACTER;
    }
    
    private static String replaceWildcardsAtBeginning(String userInput){
        return userInput.replaceAll(String.format("^[\\%s\\%s]+", LUCENE_WILDCARDS_SINGLE_CHARACTER, LUCENE_WILDCARDS_MULTI_CHARACTER), "");
    }
    
    private static String replaceWildcardsAfterWithspace(String userInput){
        return userInput.replaceAll(String.format("\\s[\\%s\\%s]+", LUCENE_WILDCARDS_SINGLE_CHARACTER, LUCENE_WILDCARDS_MULTI_CHARACTER), " ");
    }
}
